package com.twosnail.frame.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;

/**
 * Created by Administrator on 2017/7/11/011.
 */
@SuppressWarnings("serial")
public class Gym extends Model<Gym> {
    public static final Gym me = new Gym() ;

    /**
     * 获取所有品牌
     */
    public List<Gym> getGyms(){
        return me.find( "SELECT a.* FROM gym a " ) ;
    }

    /**
     * 获取登录用户下所有健身劵使用记录
     */
    public List<Record> getAllGyms(){
        return Db.find("SELECT a.*, ( SELECT count(1) FROM coupon_use_log b WHERE b.couponId IN ( SELECT couponId FROM coupon c WHERE c.gymId = a.gymId ) AND b.`status` IN (1, 2, 3))+a.counts totals, " +
                "( SELECT count(1) FROM coupon_use_log b WHERE b.couponId IN ( SELECT couponId FROM coupon c WHERE c.gymId = a.gymId ) AND b.`status` = 3 ) used, ( SELECT count(1) " +
                "FROM coupon_use_log b WHERE b.couponId IN ( SELECT couponId FROM coupon c WHERE c.gymId = a.gymId ) AND b.`status` = 2 ) yuyue FROM gym a");
    }

    public Record getGymById(long gymId){
        return Db.findFirst("SELECT a.*, ( SELECT count(1) FROM coupon_use_log b WHERE b.couponId IN ( SELECT couponId FROM coupon c WHERE c.gymId = a.gymId ) AND b.`status` IN (1, 2, 3))+a.counts totals, " +
                "( SELECT count(1) FROM coupon_use_log b WHERE b.couponId IN ( SELECT couponId FROM coupon c WHERE c.gymId = a.gymId ) AND b.`status` = 3 ) used, ( SELECT count(1) " +
                "FROM coupon_use_log b WHERE b.couponId IN ( SELECT couponId FROM coupon c WHERE c.gymId = a.gymId ) AND b.`status` = 2 ) yuyue FROM gym a where a.gymId="+gymId);
    }

    public List<Record> getAllMd(long gymId){
        return Db.find("select a.id,a.neckName,a.addr,a.mobile,(select count(1) from coupon_use_log b where b.userId = a.id)used from sys_user a where a.roleId = 10 and a.gymId ="+gymId);
    }

    /**
     * 增加团够次数
     * @param gymId
     * @param count
     * @throws Exception
     */
    public void updateCount( long gymId,int count ) throws Exception{
        Db.update( "update gym set counts = counts + ? where gymId = ?" , count, gymId ) ;
    }
}
